/**
 * Created by robin.tian on 17-6-1.
 */
var zookeeper = require('node-zookeeper-client');

function new_zk(hosts, zkClient) {
    var client = zookeeper.createClient(hosts);
    var path = process.argv[2];

    client.once('connected', function () {
        console.log("Connected to the Server.");
        client.create(path, function (error) {
            if (error) {
                console.log("Failed to create node :%s due to:%s", path, error);
            } else {
                console.log("Node: %s is successfully created.", path);
            }
            client.close();
        });
    });
    // zk.connect(function (err) {
    //     if (err) throw err;
    //     console.log("zk session established, id=%s", zk.client.id);
    // })
    // zk.close(function (err) {
    //     if (err) throw  err;
    //     console.log("zk sess close,re-int it");
    //     zkClient.zk = new_zk(hosts, zkClient);
    // })
    zkClient.zi = client;
    client.connect();
}

function ZkClient(hosts) {
    this.zk = new_zk(hosts, this);
}

module.exports.ZkClient = ZkClient